package vip.xiaonuo.smzq.modular.kcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.deom.NumSpecificationDeom;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb01Zh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb01ZhMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Ckqbg;
import vip.xiaonuo.smzq.modular.kcl.entity.Ckqsqdjs;
import vip.xiaonuo.smzq.modular.kcl.entity.Wjglb;
import vip.xiaonuo.smzq.modular.kcl.entity.Zlsc;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.CkqAlteration;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.CkqApplicationLists;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.SplcVo;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.ZlscVo;
import vip.xiaonuo.smzq.modular.kcl.mapper.CkqbgMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.CkqsqdjsMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.WjglbMapper;
import vip.xiaonuo.smzq.modular.kcl.service.ICkqsqdjsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.kcl.service.ISplcService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 采矿权申请登记书 服务实现类
 *
 * @author sqq
 * @since 2024-04-21
 */
@Service
public class CkqsqdjsServiceImpl extends ServiceImpl<CkqsqdjsMapper, Ckqsqdjs> implements ICkqsqdjsService {

    @Resource
    private CkqsqdjsMapper ckqsqdjsMapper;
    @Resource
    private WjglbMapper wjglbMapper;//文件上传关联

    @Resource
    private CkqbgMapper ckqbgMapper;//变更
    @Resource
    private Cb01ZhMapper cb01ZhMapper;//字典综合1

    @Resource
    private ISplcService iSplcService;


    @Override
    public Page<Ckqsqdjs> page(Ckqsqdjs ckqsqdjs) {
        QueryWrapper<Ckqsqdjs> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     * 添加登录信息
     * @param ckqApplicationList
     */
    @Override
    public void addiCkqsqdjsService(CkqApplicationLists ckqApplicationList) {
        //设置文件清单主键id
        String uuid = UUID.randomUUID().toString();
        //设置变更，延续等主键id
        String uuid2 = UUID.randomUUID().toString();
        //提起审批
        SplcVo splc=new SplcVo();
if(ckqApplicationList.getCkqbhlx()==1){
    //采矿权申请登记书
    Ckqsqdjs ckqsqdjs=new Ckqsqdjs();
    BeanUtil.copyProperties(ckqApplicationList, ckqsqdjs);
    ckqsqdjs.setPkid(uuid);
    ckqsqdjsMapper.insert(ckqsqdjs);

    if (ckqApplicationList.getCkqbhlx()==1){
        splc.setDjlx("变更");
        Ckqbg ckqbg=new Ckqbg();
        CkqAlteration ckqAlteration=ckqApplicationList.getCkqbg();
        BeanUtil.copyProperties(ckqAlteration, ckqbg);
       // ckqbg.setKqzb( ZbServiceImpl.getCoordinatesStr(ckqAlteration.getKqfwCoordinatesList()));
        ckqbg.setKqzb( ckqAlteration.getKqzb());
        ckqbg.setPkid(uuid2);
        ckqbg.setCkqsqid(uuid);
        ckqbgMapper.insert(ckqbg);
    }

    List<ZlscVo> zlscList=ckqApplicationList.getZlscList();
    if (zlscList!=null&&zlscList.size()>0){
        for (ZlscVo zlscVo:zlscList){
            QueryWrapper<Cb01Zh> Cb01Zh1 = new QueryWrapper<>();
            Cb01Zh1.eq("dm", zlscVo.getKyqklx());
            Cb01Zh kyqklx = cb01ZhMapper.selectOne(Cb01Zh1);
            QueryWrapper<Cb01Zh> Cb01Zh2 = new QueryWrapper<>();
            Cb01Zh2.eq("dm", zlscVo.getKyqdjlx());
            Cb01Zh kyqdjlx = cb01ZhMapper.selectOne(Cb01Zh2);
            QueryWrapper<Cb01Zh> Cb01Zh3 = new QueryWrapper<>();
            Cb01Zh3.eq("dm", zlscVo.getJdlx());
            Cb01Zh jdlx = cb01ZhMapper.selectOne(Cb01Zh3);
            //获取文件编号
            String zlbm = NumSpecificationDeom.getZlbm(kyqklx.getMc(), kyqdjlx.getMc(), jdlx.getMc(), zlscVo.getSort());

            QueryWrapper<Wjglb> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("pkid",zlscVo.getFileId());

           // Wjglb wjglbList=wjglbMapper.selectById(queryWrapper);
            Wjglb wjglbList=  wjglbMapper.selectFile(zlscVo.getFileId());
            wjglbList.setZlscId(uuid2);
            wjglbList.setWjbh(zlbm);
            wjglbList.setZlsxh(zlscVo.getSort());
            wjglbMapper.updateById(wjglbList);
        }
    }
}
        splc.setSqdjsId(uuid);
        splc.setKsmc(ckqApplicationList.getKsmc());
        splc.setKyqr(ckqApplicationList.getCkqsqr());

        splc.setXkzh(ckqApplicationList.getYckxkzh());

        iSplcService.addSplcVo(splc);
            }

    @Override
    public CkqApplicationLists getCkqApplicationLists(String ckqsqsid) {
        CkqApplicationLists ckqApplicationLists=new CkqApplicationLists();
        //查询外层申请书
        Ckqsqdjs ckqsqdjs=  ckqsqdjsMapper.selectById(ckqsqsid);
        if (ckqsqdjs==null){
            return null;
        }
        BeanUtil.copyProperties(ckqsqdjs, ckqApplicationLists);
      int type=ckqsqdjs.getCkqbhlx();
       if (type==1){//查询对应的变更数据
           QueryWrapper<Ckqbg> queryWrapperBg=new QueryWrapper<>();
           queryWrapperBg.eq("ckqsqid",ckqsqsid);
           Ckqbg ckqbg= ckqbgMapper.selectOne(queryWrapperBg);
           if (ckqbg !=null){
               //变更数据
               CkqAlteration ckqAlteration=new CkqAlteration();
               BeanUtil.copyProperties(ckqbg, ckqAlteration);

               if(ckqbg.getBglx() != null){
                   QueryWrapper<Cb01Zh> Cb01Zh1 = new QueryWrapper<>();
                   Cb01Zh1.eq("dm", ckqbg.getBglx());
                   Cb01Zh bgmc = cb01ZhMapper.selectOne(Cb01Zh1);
                   ckqAlteration.setBglxMc(bgmc.getMc());

               }
               ckqApplicationLists.setCkqbg(ckqAlteration);
               QueryWrapper<Wjglb>  wjbgQuery=new QueryWrapper<>();
               wjbgQuery.eq("zlsc_id",ckqbg.getPkid());
               List<Wjglb> wjlbList= wjglbMapper.selectList(wjbgQuery);
               if (wjlbList!=null&&wjlbList.size()>0){
                   List<ZlscVo> zlscList=new ArrayList<>();
                   for (Wjglb wjglb:wjlbList){
                       ZlscVo zlscVo=new ZlscVo();
                       zlscVo.setFileId(wjglb.getPkid());
                       zlscVo.setFileName(wjglb.getWjmc());
                       zlscVo.setSort(wjglb.getZlsxh());
                       zlscList.add(zlscVo);
                   }
                   ckqApplicationLists.setZlscList(zlscList);
               }
           }
       }
        return ckqApplicationLists;
    }
}
